/**
 * 文  件   名：HWMenuPopupWindow.java
 * 版          权： Copyright PingAn Technology All Rights Reserved.
 * 描          述：
 * 创  建   人： LIJINHUA922   E-mail:LIJINHUA922@pingan.com.cn
 * 创建时间： 2015-1-19 上午10:41:48
 *
 * 修   改  人：
 * 修改时间：
 * 修改内容：[修改内容]
 */
package com.app.jzgdirector.common.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.jzgdirector.R;


public class MineHeadPhotoSelectWindow extends PopupWindow implements OnClickListener {


	private LinearLayout mMenuView;
	private Context context;
	private OnMenuItemClick menuItemClick;
	private static final int HEAD_CAMERA_CODE = 100;
	private String PHOTO_NAME = "";
	private final int CAMERA = 1;//拍照

	private TextView camera;
	private TextView localStorage;
	private TextView cancel;
	private RoundImageView default1;
	private RoundImageView default2;
	private RoundImageView default3;
	private RoundImageView default4;

	public enum MenuItem{
		CAMERA,
		LOCAL_STORAGE,
		CANCEL,
		DEFAULT1,
		DEFAULT2,
		DEFAULT3,
		DEFAULT4
	}
	public interface OnMenuItemClick{
		public void onMenuItemClick(MenuItem menuItem);
	}

	public static MineHeadPhotoSelectWindow create(Context context){
		return new MineHeadPhotoSelectWindow(context);
	}

	public MineHeadPhotoSelectWindow(final Context context) {
		super(context);
		this.context = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View layoutView = inflater.inflate(R.layout.mine_hp_select_window, null);
		camera = (TextView) layoutView.findViewById(R.id.mine_hp_camera) ;
		localStorage = (TextView) layoutView.findViewById(R.id.mine_hp_local_image) ;
		cancel = (TextView) layoutView.findViewById(R.id.mine_hp_cancel) ;
		default1=(RoundImageView)layoutView.findViewById(R.id.iv_default_1) ;
		default2=(RoundImageView)layoutView.findViewById(R.id.iv_default_2) ;
		default3=(RoundImageView)layoutView.findViewById(R.id.iv_default_3) ;
		default4=(RoundImageView)layoutView.findViewById(R.id.iv_default_4) ;
		camera.setOnClickListener(this);
		localStorage.setOnClickListener(this);
		cancel.setOnClickListener(this);
		default1.setOnClickListener(this);
		default2.setOnClickListener(this);
		default3.setOnClickListener(this);
		default4.setOnClickListener(this);


		// 设置SelectPicPopupWindow的View
		this.setContentView(layoutView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.MATCH_PARENT);
//		 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(false);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		// this.setAnimationStyle(R.style.PopupAnimation);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView = (LinearLayout) layoutView.findViewById(R.id.pop_layout);
		mMenuView.setAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_bottom_in));
		layoutView.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = layoutView.findViewById(R.id.pop_layout).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						mMenuView.setAnimation(AnimationUtils.loadAnimation(
								context, R.anim.push_bottom_out));
						PopuwindowNewDismiss();
					}
				}
				return true;
			}
		});
	}


	@Override
	public void onClick(View v) {
		MenuItem click = MenuItem.CANCEL;
		switch (v.getId()){
			case R.id.mine_hp_camera:
				click= MenuItem.CAMERA;
				break;
			case R.id.mine_hp_local_image:
				click= MenuItem.LOCAL_STORAGE;
				break;
			case R.id.mine_hp_cancel:
				click= MenuItem.CANCEL;
				break;
			case R.id.iv_default_1:
				click= MenuItem.DEFAULT1;
				break;
			case R.id.iv_default_2:
				click= MenuItem.DEFAULT2;
				break;
			case R.id.iv_default_3:
				click= MenuItem.DEFAULT3;
				break;
			case R.id.iv_default_4:
				click= MenuItem.DEFAULT4;
				break;
			default:
				click= MenuItem.CANCEL;
				break;
		}
		mMenuView.setAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_bottom_out));
		PopuwindowNewDismiss();
		if(menuItemClick != null){
			menuItemClick.onMenuItemClick(click);
		}
	}

	public void setOnMenuItemClick(OnMenuItemClick menuItemClick) {
		this.menuItemClick = menuItemClick;
	}
	/**
	 *
	 */
	private void PopuwindowNewDismiss() {
		this.setFocusable(false);
		if(isShowing()){
			dismiss();
		}
	}


}
